package edu.whut.mall.admin.controller;


import edu.whut.mall.common.util.WxPayKit;
import edu.whut.mall.common.util.XmlUtils;
import edu.whut.mall.common.api.ResponseMap;
import edu.whut.mall.pojo.mapper.AreaMapper;
import edu.whut.mall.pojo.model.Area;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: smq
 * Unit: D9lab
 * Date: 2021-06-28 09:01
 * Description: 重定向测试接口
 */
@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
@Api(tags = {"测试接口"}, description = "测试接口", produces = MediaType.APPLICATION_JSON_VALUE)
public class TestController {

    private final AreaMapper areaMapper;

    @ApiOperation(value = "测试重定向")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/testRedirect", method = RequestMethod.POST)
    public Map<String, Object> getProductType(HttpServletRequest request) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            String temp1 = request.getParameter("file" + (i == 0 ? "" : i));
            String temp2 = request.getParameter("file" + (i == 0 ? "" : i) + "Thumb");
            if (temp1 != null && temp2 != null) {
                list.add(temp1);
                list.add(temp2);
            }
        }
        return map.putValue(list, "重定向成功");
    }


    @ApiOperation(value = "sql-foreach语句测试")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/testForeach", method = RequestMethod.POST)
    public Map<String, Object> testSql(HttpServletRequest request) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        List<Integer> list = new ArrayList<>();
        list.add(110101);
        list.add(110115);
        List<Area> test = areaMapper.testSqlForeach(list);
        return map.putValue(test, "测试成功");
    }


    @ApiOperation(value = "sql-choose语句测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "", paramType = "form", dataType = "int", required = false),
            @ApiImplicitParam(name = "name", value = "", paramType = "form", dataType = "string", required = false)
    })
    @RequestMapping(value = "/testChoose", method = RequestMethod.POST)
    public Map<String, Object> testChoose(HttpServletRequest request, Integer code, String name) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        Map<String, Object> maps = new HashMap<>();
        maps.put("code", code);
        maps.put("name", name);
        List<Area> test = areaMapper.testSqlChoose(maps);
        return map.putValue(test, "测试成功");
    }


    @ApiOperation(value = "ip测试")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/getClientIp", method = RequestMethod.POST)
    public Map<String, Object> getClientIp(HttpServletRequest request) {
        ResponseMap map = ResponseMap.getInstance();
        String ip = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = ip.indexOf(",");
            if (index != -1) {
                return map.putValue(ip.substring(0, index), "获取成功");
            } else {
                return map.putValue(ip, "获取成功");
            }
        }
        ip = request.getHeader("X-Real-IP");
        if (StringUtils.isNotEmpty(ip) && !"unKnown".equalsIgnoreCase(ip)) {
            return map.putValue(ip, "获取成功");
        }
        return map.putValue(request.getRemoteAddr(), "获取失败");
    }


    @ApiOperation(value = "字符串、xml格式测试")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/testXml", method = RequestMethod.POST)
    public Map<String, Object> testXml(HttpServletRequest request) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        String result = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
                "<root>\n<name>我是徐茅山</name>\n<age>今年20岁</age>\n<sex>男</sex>\n</root>";
        Map<String, String> xmlBodyContext = WxPayKit.toMap(result);
        for (Map.Entry<String, String> entry : xmlBodyContext.entrySet()) {
            log.info("属性： {} ，值： {}", entry.getKey(), entry.getValue());
        }
        Map<String, String> maps = new HashMap<>();
        maps.put("姓名", "smq");
        maps.put("年龄", "23");
        maps.put("性别", "男");
        String test = WxPayKit.toXml(maps);
        log.info(test);
        return map.putSuccess("测试成功");
    }


    @ApiOperation(value = "转文件测试")
    @ApiImplicitParams({
    })
    @RequestMapping(value = "/testDoc", method = RequestMethod.POST)
    public Map<String, Object> testDoc(HttpServletRequest request) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        String result = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
                "<root>\n<name>我是徐茅山</name>\n<age>今年20岁</age>\n<sex>男</sex>\n</root>";
        Document doc = XmlUtils.parseXmlString(result);
        System.err.println("===============" + doc);
        return map.putSuccess("测试成功");
    }

    @ApiOperation(value = "智能文档插件测试数据")
    @RequestMapping(value = "/testData", method = RequestMethod.GET)
    public Map<String, Object> testData(HttpServletRequest request, Integer current) throws Exception {
        ResponseMap map = ResponseMap.getInstance();
        if (current == 1) return map.putValue("{\"current\":1000,\"list\":[" +
                "{\"PLANT\":\"3011\",\"RESP_DEPT\":\"物资主数据管理处\",\"RESP_SUB_DEPT\":\"运维项目科\",\"MATERIAL_STATUS_PLANT\":\"D2\",\"MATERIAL_NBR\":\"90010150873011\",\"RESP_ID\":\"20076090,123\",\"RESP_NAME\":\"李清\"}," +
                "{\"PLANT\":\"3011\",\"RESP_DEPT\":\"物资主数据管理处\",\"RESP_SUB_DEPT\":\"运维项目科\",\"MATERIAL_STATUS_PLANT\":\"D2\",\"MATERIAL_NBR\":\"90010154183011\",\"RESP_ID\":\"20076090,李清\",\"RESP_NAME\":\"李清\"}]," +
                "\"pages\":10000,\"size\":25,\"total\":250000}\"", "测试成功");
        else if (current == 2) return map.putValue("{\"current\":1000,\"list\":[" +
                "{\"PLANT\":\"3011\",\"RESP_DEPT\":\"物资主数据管理处\",\"RESP_SUB_DEPT\":\"运维项目科\",\"MATERIAL_STATUS_PLANT\":\"D2\",\"MATERIAL_NBR\":\"90010150877811\",\"RESP_ID\":\"20076090\",\"RESP_NAME\":\"李清\"}," +
                "{\"PLANT\":\"3011\",\"RESP_DEPT\":\"物资主数据管理处\",\"RESP_SUB_DEPT\":\"运维项目科\",\"MATERIAL_STATUS_PLANT\":\"D2\",\"MATERIAL_NBR\":\"90010154120011\",\"RESP_ID\":\"20076090\",\"RESP_NAME\":\"李清\"}]," +
                "\"pages\":10000,\"size\":25,\"total\":250000}\"", "测试成功");
        else return null;
    }
}
